<?php
	namespace Pages;
	class Post implements Page {
		private $id;
		public function __construct(){
		}
		public function exec($data){
			$this->id = (isset($data['id'])?$data['id']:null);
			$m = \Model\Model::getInstance();
			if($this->id == null || !$m->postExists($this->id)){
				return 'index.html';
			}
		}
		public function toXML(){
			$xml = new \DOMDocument();
			$xml->load('Pages/Post.xml');
			$m = \Model\Model::getInstance();
			$post = $m->getPost($this->id);
			$title = $xml->getElementsByTagName('title')->item(0);
			$title->nodeValue .= ' '.$this->id.' de '.$post->userPseudo;

			$xmlPost = $xml->createElement('post');
			$xmlPost->setAttribute('url','http://'.$_SERVER['SERVER_NAME'].'/Post-'.$post->id.'.html');
			$xmlPost->setAttribute('id',$post->id);
			$author = $xml->createElement('author');
			$author->setAttribute('id',$post->userId);
			$author->appendChild($xml->createTextNode($post->userPseudo));
			$xmlPost->appendChild($author);
			$message = $xml->createElement('message');
			$message->appendChild($xml->createTextNode($post->message));
			$xmlPost->appendChild($message);
			$date = $xml->createElement('date');
			$date->appendChild($xml->createTextNode($post->date));
			$xmlPost->appendChild($date);
			$xml->appendChild($xmlPost);
			return $xml;
		}
		public function getXSL($format){
			$xml = new \DOMDocument();
			$xml->load('XSL/html/Post.xsl');
			return $xml;
		}
	}
?>
